<?php

namespace application\member\controller;

use ticky\controller;

class other extends controller {

    /**
     * QQ登录
     */
    public function qq_login() {
        $appid = get_config('qq_app_id');
        $appkey = get_config('qq_app_key');
        $callback = url('member/other/qq_login'); //回调地址 默认此方法，不能更改
        if ($appid == '' || $appkey == '') {
            showmsg('QQ配置项为空，请联系管理员！', 'stop');
        }
        load_extend('qqapi.class.php');
        $qq_info = new qqapi($appid, $appkey, $callback);

        if (!isset($_GET['code'])) {
            $qq_info->redirect_to_login();
        } else {
            $code = $_GET['code'];
            $_SESSION['fromlogin'] = 'qq';
            $openid = $_SESSION['openid'] = $qq_info->get_openid($code);
            if (!empty($openid)) {
                $userid = db('member_authorization')->field('uid')->where(array('authname' => 'qq', 'token' => $openid))->order('id DESC')->one();
                if ($userid) {
                    $this->_login($userid);
                } else {
                    $userinfo = $qq_info->get_user_info();
                    $userinfo = array(
                        'nickname' => $userinfo['nickname'],
                        'userpic' => $userinfo['figureurl_qq_2'],
                        'address' => $userinfo['province'] . $userinfo['city']
                    );

                    session('userinfo', $userinfo);
                    session('connectid', $openid);
                    session('fromlogin', 'qq');
                    session('fromname', 'QQ');

                    include template('member', 'bind');
                }
            } else if (isset($_SESSION['userinfo'])) {  //防止刷新
                $userinfo = session('userinfo');
                include template('member', 'bind');
            }
        }
    }

    /**
     * 微博登录
     */
    public function weibo_login() {
        $appid = get_config('weibo_key');
        $appkey = get_config('weibo_secret');
        $callback = url('member/other/weibo_login'); //回调地址 默认此方法，不能更改

        load_extend('saetv2.ex.class.php');
        if ($appid == '' || $appkey == '') {
            showmsg('微博配置项为空，请联系管理员！', 'stop');
        }
        $obj = new SaeTOAuthV2($appid, $appkey);
        if (!isset($_GET['code'])) {
            $redirect = $obj->getAuthorizeURL($callback);
            header("Location:$redirect");
        } else {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = $callback;
            try {
                $token = $obj->getAccessToken('code', $keys);
            } catch (OAuthException $e) {

            }
            if ($token)
                $_SESSION['token'] = $token;

            $sa = new SaeTClientV2($appid, $appkey, $_SESSION['token']['access_token']);
            $uid_get = $sa->get_uid();
            $uid = $uid_get['uid'];
            $info = $sa->show_user_by_id($uid); //根据ID获取用户等基本信息
            if (empty($info['id'])) {
                showmsg('获取用户信息失败！', url('member/index/login'));
            }

            $userid = db('member_authorization')->field('uid')->where(array('authname' => 'weibo', 'token' => $info['id']))->order('id DESC')->one();
            if ($userid) {
                $this->_login($userid);
            } else {
                $userinfo = array(
                    'nickname' => $info['name'],
                    'userpic' => $info['avatar_large'],
                    'address' => $info['location']
                );
                $_SESSION['userinfo'] = $userinfo;
                $_SESSION['connectid'] = $info['id'];
                $_SESSION['fromlogin'] = 'weibo';
                $_SESSION['fromname'] = '微博';
                include template('member', 'bind');
            }
        }
    }

}
